package com.imooc.mall.controller;

import com.imooc.mall.consts.MallConst;
import com.imooc.mall.consts.TokenCache;
import com.imooc.mall.enums.ResponseEnum;
import com.imooc.mall.form.UserLoginForm;
import com.imooc.mall.form.UserRegisterForm;
import com.imooc.mall.pojo.User;
import com.imooc.mall.service.IUserService;
import com.imooc.mall.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.UUID;


@RestController
@Slf4j
public class UserController {

    @Autowired
    IUserService userService;
    
    @PostMapping("/user/register")
    public ResponseVo register(@Valid @RequestBody UserRegisterForm userForm, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            log.error("注册提交的参数有误,{} {}",
                    bindingResult.getFieldError().getField(),
                    bindingResult.getFieldError().getDefaultMessage());
            return ResponseVo.error(ResponseEnum.PARAM_ERROR,bindingResult);
        }

        User user=new User();
        BeanUtils.copyProperties(userForm,user);
        return userService.register(user);

    }

    @PostMapping("/user/login")
    public ResponseVo<User> login(@Valid @RequestBody UserLoginForm userLoginForm, BindingResult bindingResult,
                                  HttpSession session){

        String forgetToken = UUID.randomUUID().toString();
        //本地缓存
        TokenCache.setKey(TokenCache.TOKEN_PREFIX+userLoginForm.getUsername(),forgetToken);
        if (bindingResult.hasErrors()){

            return ResponseVo.error(ResponseEnum.PARAM_ERROR,bindingResult);
        }
        ResponseVo<User> userResponseVo = userService.login(userLoginForm.getUsername(), userLoginForm.getPassword());

        //设置session
        session.setAttribute(MallConst.CURRENT_USER,userResponseVo.getData());
        session.setAttribute(MallConst.TOKEN,forgetToken);
    /*    log.info("Token为: {}",forgetToken);*/

        return userResponseVo;

    }

    //session保存在内存里，缺点明显， 一般保存在token+redis
    @GetMapping("/user")
    public ResponseVo<User> userInfo(HttpSession session){
       /* if(StringUtils.isBlank(forgetToken)){
            return ResponseVo.error(ResponseEnum.ERROR,"参数错误，Token需要被传递");
        }*/

        User user=(User) session.getAttribute(MallConst.CURRENT_USER);
        log.info("Token为: {}",session.getAttribute(MallConst.TOKEN));
        return ResponseVo.success(user);
       /* String token = TokenCache.getKey(TokenCache.TOKEN_PREFIX+user.getUsername());*/
      /*  if(org.apache.commons.lang3.StringUtils.isBlank(token)){
            return ResponseVo.error(ResponseEnum.ERROR,"token无效或者过期");
        }*/
        /*if(org.apache.commons.lang3.StringUtils.equals(forgetToken,token)){*/

      /*  }
        return ResponseVo.error(ResponseEnum.ERROR,"token错误,请重新获取重置密码的token");
*/
    }

    @PostMapping("/user/logout")
    public ResponseVo logout(HttpSession session){
        log.info("/user/logout sessionId={}",session.getId());
        session.removeAttribute(MallConst.CURRENT_USER);
        return ResponseVo.success();
    }

}
